package com.tools.cloud.consumer.controller;

import com.tools.cloud.api.model.CommonResult;
import com.tools.cloud.api.model.Payment;
import com.tools.cloud.consumer.config.ResturlProperties;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.stereotype.Controller;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;

import java.util.List;

@Controller
@RequestMapping("/payment")
@Slf4j
public class PaymentConsumerController {
    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private ResturlProperties prop;

    @RequestMapping(value = "/selectOne", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult<Payment> selectOne(Payment param) {
        CommonResult<Payment> result = new CommonResult<Payment>();
        try {
            MultiValueMap<String, Object> map = new LinkedMultiValueMap<>();
            map.add("id", param.getId().toString());
            map.add("serial", param.getSerial());

            return restTemplate.postForEntity(prop.getPaymentpath()+"selectOne", map, CommonResult.class).getBody();
        } catch(Exception e) {
            log.error("", e);
            return CommonResult.getErrObj("查询异常！");
        }
    }

    @RequestMapping(value = "/selectList", method = RequestMethod.POST)
    @ResponseBody
    public List<Payment> selectList(Payment param) {
        MultiValueMap<String, Object> map = new LinkedMultiValueMap<>();
        map.add("id", param.getId().toString());
        map.add("serial", param.getSerial());

        return restTemplate.postForEntity(prop.getPaymentpath()+"selectList", map, List.class).getBody();
    }

    @RequestMapping(value = "/updateById", method = RequestMethod.POST)
    @ResponseBody
    public Integer updateById(Payment param) {
        MultiValueMap<String, Object> map = new LinkedMultiValueMap<>();
        map.add("id", param.getId().toString());
        map.add("serial", param.getSerial());

        return restTemplate.postForEntity(prop.getPaymentpath()+"updateById", map, Integer.class).getBody();
    }

    @RequestMapping(value = "/insert", method = RequestMethod.POST)
    @ResponseBody
    public Integer insert(Payment param) {
        MultiValueMap<String, Object> map = new LinkedMultiValueMap<>();
        map.add("id", param.getId().toString());
        map.add("serial", param.getSerial());

        return restTemplate.postForEntity(prop.getPaymentpath()+"insert", map, Integer.class).getBody();
    }

    @RequestMapping(value = "/deleteById", method = RequestMethod.POST)
    @ResponseBody
    public Boolean deleteById(Payment param) {
        MultiValueMap<String, Object> map = new LinkedMultiValueMap<>();
        map.add("id", param.getId().toString());

        return restTemplate.postForEntity(prop.getPaymentpath()+"deleteById", map, Boolean.class).getBody();
    }
}